package com.example.test.netty.utils;

import sun.misc.BASE64Decoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

/**
 * @program: test
 * @description: 图片base64转换工具
 * @author: qiu bo yang
 * @create: 2020-10-14 15:18
 **/
public class ImageBase64Utils {
    /**
     * base64字符串转图片文件
     *
     * @param base64String
     */
    public static void base64StringToImage(String base64String) {
        try {
            BASE64Decoder base64Decoder = new BASE64Decoder();
            byte[] bytes = base64Decoder.decodeBuffer(base64String);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            BufferedImage bufferedImage = ImageIO.read(byteArrayInputStream);
            File file = new File("d:/sn/" + System.currentTimeMillis() + ".jpg");
            ImageIO.write(bufferedImage, "jpg", file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
